using System;
using System.Threading;

namespace BoxBinary.AspectCache.Core.ReplicationSink
{
    internal sealed class SinkMonitorThreadStart : IDisposable
    {
        private Thread _monitorThread;
        private ThreadStart _monitorThreadStart;

        internal SinkMonitorThreadStart(SinkMonitorThread worker)
        {
            Start(worker);
        }

        #region IDisposable Members

        public void Dispose()
        {
            try
            {
                Stop();
            }
            catch
            {
            }
        }

        #endregion

        internal void Start(SinkMonitorThread worker)
        {
            Stop();

            _monitorThreadStart = worker.Start;
            _monitorThread = new Thread(_monitorThreadStart);
            _monitorThread.Name = "ReplicationSinkMonitor";
            _monitorThread.IsBackground = true;
            _monitorThread.Priority = ThreadPriority.Lowest;
            _monitorThread.Start();
        }

        internal void Stop()
        {
            if (!(_monitorThread == null))
                _monitorThread.Abort();
        }
    }
}